<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-08-14 18:46:03
 * @version $Id$
 */
namespace app\finance\controller;
use think\Db;
use think\Request;
/**
 * 数据统计
 */
class Mstatistics extends Base {
    
    function index(){
        $iw=Request()->param();

    	$start_time=isset($iw['start_time'])?strtotime($iw['start_time']):'';
    	$end_time=isset($iw['end_time'])?strtotime($iw['end_time']):'';

    	if($end_time){
            $_end_time=getOneDay($end_time);
            // $_end_time=$_end_time['et'];
            if ($end_time==$_end_time['st']) {
                $end_time=$_end_time['et'];
            }
        }else{
            $end_time=getOneDay();
            $end_time=$end_time['et'];
        }

        if ($start_time) {
            // $start_time=getOneDay($start_time);
            // $start_time=$start_time['st'];
        }else{
            $start_time=getDayRegion($end_time,30);
            $start_time=$start_time['st'];
        }
        
        $iw['start_time']=date('Y-m-d H:i:s',$start_time);
        $iw['end_time']=date('Y-m-d H:i:s',$end_time);

        if ($iw['selt']!=='0') {
            $iw['selt']=$iw['selt']?:9;
        }
    	$data['w']=$iw;

        $tmp_orders = Db::name('order')->where("pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $orders = [];
        foreach ($tmp_orders as $k => $v) {
            $orders[date('Y-m-d',strtotime($v['pay_time']))] += $v['pay_money']; 
        }
        $dayMap=getDayMapSE($start_time,$end_time);
        $tmap = ''; $mmap = '';
        foreach ($dayMap as $k => $v) {
           $tmap .= '"'.date('Y-m-d',$k).'",';
           $mmap .= '"'.$orders[date('Y-m-d',$k)].'",';
        }
        $tmap = '['.rtrim($tmap,',').']';
        $mmap = '['.rtrim($mmap,',').']';

        $data['tmap']=$tmap;
        $data['mmap']=$mmap;

    	$tmp = Db::field('og.goods_id,og.goods_name,og.brand_name,sum(og.num) as num')
    	->table('sdzx_order_goods')->alias('og')
    	->join('sdzx_order o','og.order_id=o.order_id')
    	->where("o.pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")
    	->group('og.goods_id')
    	->order("sum(og.num) desc")
    	->limit(50)
    	->select();

    	$list = array_reverse($tmp);
    	$yAxis = ''; $series='';
    	foreach ($list as $k => $v) {
    		$yAxis .= '"'. $v['goods_name'] .'('. $v['brand_name'] . ')",';
    		$series .= $v['num'] .',';
    	}
    	$yAxis = '['.rtrim($yAxis,',').']';
    	$series = '['.rtrim($series,',').']';
    	$data['goods']['count'] = sizeof($list);
    	$data['goods']['yAxis'] = $yAxis;
    	$data['goods']['series'] = $series;

    	$tmpOrder = Db::field('receiver_name,count(order_id) as num,s.shop_name')
    	->table('sdzx_order')->alias('o')
    	->join('sdzx_shop s','o.user_id=s.user_id','left')
    	->where("o.pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")
    	->group('o.user_id')
    	->order('count(o.order_id) desc')
    	->limit(50)->select();

    	$listorder = array_reverse($tmpOrder);
    	$oyAxis = ''; $oseries='';
    	foreach ($listorder as $k => $v) {
    		$oyAxis .= '"'. $v['receiver_name'] .'('. $v['shop_name'] . ')",';
    		$oseries .= $v['num'] .',';
    	}
    	$oyAxis = '['.rtrim($oyAxis,',').']';
    	$oseries = '['.rtrim($oseries,',').']';
    	$data['uorder']['count'] = sizeof($listorder);
    	$data['uorder']['yAxis'] = $oyAxis;
    	$data['uorder']['series'] = $oseries;
    	
    	return $this->fetch('',$data);
    }
}